package com.basic.modules.ykf.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.basic.common.annotation.StringTrim;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.Query;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.sys.controller.AbstractController;
import com.basic.modules.ykf.entity.YkfCustomerConsultRecordEntity;
import com.basic.modules.ykf.service.YkfCustomerConsultRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfCustomerConsultEntity;
import com.basic.modules.ykf.service.YkfCustomerConsultService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 客户咨询记录管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-04-28 20:48:40
 */
@RestController
@RequestMapping("ykf/customerConsult")
public class YkfCustomerConsultController extends AbstractController {

    @Autowired
    private YkfCustomerConsultService ykfCustomerConsultService;

    @Autowired
    private YkfCustomerConsultRecordService ykfCustomerConsultRecordService;

    /**
     * 列表-分页
     */
    @StringTrim
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfCustomerConsultEntity> params){
        PageUtils page = ykfCustomerConsultService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfCustomerConsultEntity param){
        if (ObjectUtil.isEmpty(param) || ObjectUtil.isEmpty(param.getConsultId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfCustomerConsultService.getById(param.getConsultId()));
    }

    /**
     * 收藏
     */
    @PostMapping("/favorite")
    public R favorite(@RequestBody YkfCustomerConsultEntity ykfCustomerConsult){
        if (ObjectUtil.isNotEmpty(ykfCustomerConsult.getConsultId())) {
            YkfCustomerConsultEntity consultEntity = ykfCustomerConsultService.getById(ykfCustomerConsult.getConsultId());
            String favoriteUser = consultEntity.getFavoriteUser();
            Long userId = getUserId();
            if (ykfCustomerConsult.getFavoriteStatus() == 1) {
                if (StrUtil.isNotBlank(favoriteUser)) {
                    favoriteUser = favoriteUser + "," + userId;
                } else {
                    favoriteUser = StrUtil.toString(userId);
                }
            } else {
                if (StrUtil.isNotBlank(favoriteUser)) {
                    //去除收藏用户
                    List<String> users = new ArrayList<>();
                    List<String> split = StrUtil.split(favoriteUser, ',');
                    for (String s : split) {
                        if (!StrUtil.equals(s, StrUtil.toString(userId))) {
                            users.add(s);
                        }
                    }
                    favoriteUser = StrUtil.join(",", users);
                }
            }

            UpdateWrapper<YkfCustomerConsultEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(YkfCustomerConsultEntity::getConsultId, ykfCustomerConsult.getConsultId())
                    .set(YkfCustomerConsultEntity::getFavoriteUser, favoriteUser);
//                    .set(YkfCustomerConsultEntity::getFavoriteStatus, ykfCustomerConsult.getFavoriteStatus());
            ykfCustomerConsultService.update(updateWrapper);
        }
        return R.ok();
    }

    /**
     * 归档
     */
    @PostMapping("/archiving")
    public R archiving(@RequestBody YkfCustomerConsultEntity ykfCustomerConsult){
        if (ObjectUtil.isNotEmpty(ykfCustomerConsult.getConsultId())) {
            UpdateWrapper<YkfCustomerConsultEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(YkfCustomerConsultEntity::getConsultId, ykfCustomerConsult.getConsultId())
                    .set(YkfCustomerConsultEntity::getArchivingStatus, ykfCustomerConsult.getArchivingStatus());
            ykfCustomerConsultService.update(updateWrapper);
        }
        return R.ok();
    }

    /**
     * 删除负责人
     */
    @PostMapping("/deleteMaster")
    public R deleteMaster(@RequestBody YkfCustomerConsultEntity ykfCustomerConsult){
        if (ObjectUtil.isNotEmpty(ykfCustomerConsult.getConsultId())) {
            UpdateWrapper<YkfCustomerConsultEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(YkfCustomerConsultEntity::getConsultId, ykfCustomerConsult.getConsultId())
                    .set(YkfCustomerConsultEntity::getMasterName, "");
            ykfCustomerConsultService.update(updateWrapper);
        }
        return R.ok();
    }

    /**
     * 移交负责人
     */
    @PostMapping("/changeMaster")
    public R changeMaster(@RequestBody YkfCustomerConsultEntity ykfCustomerConsult){
        if (ObjectUtil.isNotEmpty(ykfCustomerConsult.getConsultId())) {
            UpdateWrapper<YkfCustomerConsultEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(YkfCustomerConsultEntity::getConsultId, ykfCustomerConsult.getConsultId())
                    .set(YkfCustomerConsultEntity::getMasterName, ykfCustomerConsult.getMasterName());
            ykfCustomerConsultService.update(updateWrapper);
        }
        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfCustomerConsultEntity ykfCustomerConsult){
        ValidatorUtils.validateEntity(ykfCustomerConsult);
		ykfCustomerConsultService.save(ykfCustomerConsult);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfCustomerConsultEntity ykfCustomerConsult){
        ValidatorUtils.validateEntity(ykfCustomerConsult);
		ykfCustomerConsultService.updateById(ykfCustomerConsult);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] consultIds){
		ykfCustomerConsultService.removeByIds(Arrays.asList(consultIds));
        return R.ok();
    }

    /**
     * 保存跟进记录
     */
    @PostMapping("/addConsultRecord")
    public R addConsultRecord(@RequestBody YkfCustomerConsultRecordEntity params){
        ykfCustomerConsultRecordService.save(params);
        return R.ok();
    }

    /**
     * 查询跟进记录
     */
    @PostMapping("/getConsultRecordList")
    public R getConsultRecordList(@RequestBody YkfCustomerConsultRecordEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getConsultId())) {
            throw new RRException("参数缺失");
        }
        QueryWrapper<YkfCustomerConsultRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YkfCustomerConsultRecordEntity::getConsultId, params.getConsultId())
                .orderByDesc(YkfCustomerConsultRecordEntity::getRecordDate);
        return R.ok(ykfCustomerConsultRecordService.list(queryWrapper));
    }
}
